package com.cdd.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdd.index.config.NewPageHelp;
import com.cdd.index.feign.GoodsFeign;
import com.cdd.index.mapper.ZoneGoodsMapper;
import com.cdd.index.service.IZoneGoodsService;
import com.common.entity.Goods;
import com.common.entity.Zone;
import com.common.entity.ZoneGoods;
import com.common.untils.CommonResult;

import com.common.vo.GoodsVo;
import com.common.vo.ShopVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzl
 * @since 2022-09-09
 */
@Service
public class ZoneGoodsServiceImpl extends ServiceImpl<ZoneGoodsMapper, ZoneGoods> implements IZoneGoodsService {

    @Autowired
    private ZoneGoodsMapper zoneGoodsMapper;

    @Autowired
    private GoodsFeign goodsFeign;

    @Override
    public CommonResult getAllDetails(Integer currentPage, Integer pageSize, ZoneGoods zoneGoods) {
        System.out.println("zoneGoods:"+zoneGoods.getGname());
        String zoneId = zoneGoods.getZoneId();
        QueryWrapper<ZoneGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("zone_id",zoneId);
        List<ZoneGoods> zoneGoods1 = zoneGoodsMapper.selectList(wrapper);
        List<String> goodsId = new ArrayList<>();
        for(ZoneGoods zg:zoneGoods1){

            goodsId.add(zg.getGoodsId());
        }
        List<GoodsVo>  commonResult = goodsFeign.selectGoodsZone();
        System.out.println("++++++++++++++++++common"+commonResult);

        List<GoodsVo> g2 = new ArrayList<>();
        for (String id: goodsId) {
            for (GoodsVo g1:commonResult){
                if (id.equals(g1.getGoodsId())){
                    g2.add(g1);
                }
            }
        }
        PageInfo pageInfo = NewPageHelp.pageHelper(g2,currentPage,pageSize);
        return new CommonResult(2000,"查询成功",pageInfo);
    }


    @Override
    public CommonResult addZoneGoods(String zoneId,String id,String ids ) {
        ZoneGoods zoneGoods = new ZoneGoods();
        zoneGoods.setZoneId(zoneId);
        zoneGoods.setGoodsId(id);
        zoneGoods.setIsDeleted(0);
        zoneGoods.setIsDisable(0);
        zoneGoods.setGoodsShopId(ids);
        zoneGoodsMapper.insert(zoneGoods);
        System.out.println("aaaaaaaaaaaaaaaaaaaaa"+id);
        Integer updateById = goodsFeign.updateById(id);


        return new CommonResult(2000,"添加成功",null);


    }

    @Override
    public CommonResult deleteZoneGoods(String id) {
        Integer i=zoneGoodsMapper.deleteZoneGoods(id);
        zoneGoodsMapper.updateByGoodsId(id);
        return new CommonResult(2000,"删除成功",null);
    }



}
